package com.lyw.mine.vm

import android.app.Activity
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.lu.library.base.BaseViewModel
import com.lu.library.base.IUiIntent
import com.lu.library.base.IUiState
import com.lu.library.kv.KVDelegate
import com.lu.library.utils.EventBusUtils
import com.lu.library.widget.WheelDateDialog
import com.lu.library.widget.WheelViewDialog
import com.lyw.login_api.LoginConstants
import com.lyw.login_api.bean.UserInfo
import com.lyw.mine.UserInfoManager
import com.lyw.mine.bean.UserInfoUpdateEvent


data class UiUserInfo(
    var name: String = "",
    var sex:String?=null,
    var age:String?=null,
    var birthday: String?=null,
    var height:String?=null,
    var weight:String? = null,
    var head:String ? = null,
)

sealed class EditUserInfoUiState: IUiState {
    class INIT(val userInfo: UiUserInfo) : EditUserInfoUiState()
}

sealed class EditUserInfoIntent: IUiIntent {
    class EditUserName(val name:String) : EditUserInfoIntent()
    class EditUserAge(val age:Int) : EditUserInfoIntent()
    class EditUserSex(val sex:Int) : EditUserInfoIntent()
    class EditUserHeight(val height:Int) : EditUserInfoIntent()
    class EditUserWeight(val weight:Int) : EditUserInfoIntent()
    class EditUserBirthday(val birthday:String) : EditUserInfoIntent()
    class EditUserHead(val head:String) : EditUserInfoIntent()
    class ShowUserSexDialog(val activity:Activity):EditUserInfoIntent()
    class ShowUserHeightDialog(val activity:Activity):EditUserInfoIntent()
    class ShowUserWeightDialog(val activity:Activity):EditUserInfoIntent()
    class ShowUserBirthDayDialog(val activity:Activity):EditUserInfoIntent()
}

/**
 * 编辑用户信息
 */
class EditUserInfoVm:BaseViewModel<EditUserInfoUiState,EditUserInfoIntent>() {

//    private var userInfoStr by KVDelegate(key=LoginConstants.KEY_USER_INFO, def="")
//    private var userInfo :UserInfo = if(userInfoStr.isEmpty()){UserInfo()}else{GsonUtils.fromJson(userInfoStr,UserInfo::class.java)}

    init {
    }
    private var _userInfo:UserInfo = UserInfoManager.userInfo.value!!
    var userInfoObserver: Observer<UserInfo> = object: Observer<UserInfo> {
        override fun onChanged(value: UserInfo) {
            _userInfo = value
            LogUtils.d("onChanged $value")
            update(value)
        }

    }
    init {
        UserInfoManager.userInfo.observeForever(userInfoObserver)
    }

    override fun onCleared() {
        super.onCleared()
        UserInfoManager.userInfo.removeObserver(userInfoObserver)
    }
    private fun update(userInfo: UserInfo){
        updateUiState {
            EditUserInfoUiState.INIT(UiUserInfo(
                userInfo.name,
                sex=if(userInfo.sex==0){"男"}else{"女"},
                age=userInfo.age.toString(),
                height = userInfo.height.toString()+" cm",
                weight = userInfo.weight.toString()+" kg",
                birthday = if (userInfo.year==0){"未选择"}else{userInfo.year.toString()+"/"+String.format("%02d",userInfo.month)+"/"+String.format("%02d",userInfo.day)},
                head = userInfo.headUrl
            ))

        }
    }
    override fun initUiState(): EditUserInfoUiState {
        return EditUserInfoUiState.INIT(userInfo = UiUserInfo(""))
    }

    override fun handleIntent(intent: EditUserInfoIntent) {
        LogUtils.d(intent)
        when(intent) {
            is EditUserInfoIntent.ShowUserSexDialog ->{
                var data = listOf("男","女")
                WheelViewDialog(intent.activity,data,_userInfo.sex){index->
//                    userInfo.sex = index
                    UserInfoManager.updateUserInfo {
                        this.copy(sex=index)
                    }
                }.show()
            }
            is EditUserInfoIntent.ShowUserHeightDialog->{
                var data = (150..220).toList()
                var index = data.indexOf(_userInfo.height)
                WheelViewDialog(intent.activity,data,if(index==-1){0}else{index}){
                    UserInfoManager.updateUserInfo {
                        this.copy(height=data[it])
                    }
                }.show()
            }
            is EditUserInfoIntent.ShowUserWeightDialog->{
                var data = (50..120).toList()
                var index = data.indexOf(_userInfo.weight)
                WheelViewDialog(intent.activity,data,if(index==-1){0}else{index}){
//                    userInfo.weight = data[it]
//                    userInfoStr = GsonUtils.toJson(userInfo)

                    UserInfoManager.updateUserInfo {
                        this.copy(weight=data[it])
                    }
                }.show()
            }
            is EditUserInfoIntent.ShowUserBirthDayDialog->{

                WheelDateDialog(intent.activity,_userInfo.year,_userInfo.month,_userInfo.day, onSelectedCallback = {y,m,d->
//                    userInfo.year = y
//                    userInfo.month = m
//                    userInfo.day = d
                    UserInfoManager.updateUserInfo {
                        this.copy(year=y, month = m, day = d)
                    }
                }).show()
            }
            is EditUserInfoIntent.EditUserName -> {
                if (_userInfo.name!=intent.name) {
                    _userInfo.name = intent.name
                    UserInfoManager.updateUserInfo {
                        this.copy(name = intent.name)
                    }
                }
            }
            is EditUserInfoIntent.EditUserAge -> {
                _userInfo.age = intent.age
            }
            is EditUserInfoIntent.EditUserSex -> {
                _userInfo.sex = intent.sex
            }
            is EditUserInfoIntent.EditUserHeight -> {
                _userInfo.height = intent.height
            }
            is EditUserInfoIntent.EditUserWeight -> {
                _userInfo.weight = intent.weight
            }
            is EditUserInfoIntent.EditUserBirthday -> {
                _userInfo.birthday = intent.birthday
            }
            is EditUserInfoIntent.EditUserHead -> {
                _userInfo.headUrl = intent.head
            }
            else -> {}
        }
    }
}